import { Component, OnInit } from '@angular/core';
import { WinelistService } from 'src/app/service/winelist.service';
import { CommonService } from 'src/app/service/common.service';
import { ComponentBasic } from 'src/app/app.component';
import { NzMessageService, UploadFile } from 'ng-zorro-antd';
import { config } from '../../../config';
@Component({
  selector: 'app-winemanage',
  templateUrl: './winemanage.component.html',
  styleUrls: ['./winemanage.component.scss'],
  providers: [WinelistService, CommonService]
})
export class WinemanageComponent extends ComponentBasic implements OnInit {
  loading = true;
  pageIndex = 1;
  pageSize = 15;
  total = 1;
  name: any;
  doc: any = {};
  dataSet = [];
  isVisible = false;
  isEditVisible = false;
  isOkLoading = false;
  status: any;
  type: any;
  typelists = [{ key: 1, value: '酒' }, { key: 2, value: '饮料' }];
  typelist2 = [{ key: null, value: '全部' }, { key: 1, value: '酒' }, { key: 2, value: '饮料' }];
  statuslist = [{ key: null, value: '全部' }, { key: 1, value: '已上架' }, { key: 0, value: '已下架' }];
  action = config.imageuploadurl;
  private getBase64(img: File, callback: (img: string) => void): void {
    const reader = new FileReader();
    reader.addEventListener('load', () => callback(reader.result.toString()));
    reader.readAsDataURL(img);
  }
  constructor(private winelist: WinelistService, private msg: NzMessageService, private commonservice: CommonService) {
    super();
    super.setLayout(true, true);
  }

  ngOnInit() {
    this.searchData();
  }

  searchData(reset: boolean = false): void {
    this.loading = true;
    this.winelist.query({
      name: this.name,
      type: this.type,
      status: this.status,
      offset: this.pageIndex,
      limit: this.pageSize
    }).then((res) => {
      this.loading = false;
      this.total = res.total;
      this.dataSet = res.rows;
    });
  }

  add() {
    this.doc = {};
    this.isVisible = true;

  }
  handleCancel() {
    this.isVisible = false;
    this.doc = {}
  }

  handleOk() {
    this.winelist.save(this.doc).then((r) => {
      this.isVisible = false;
      this.msg.info('操作成功');
      this.searchData();
    });
  }

  edit(data: any) {
    this.isVisible = true;
    this.doc = data;
    // this.winelist.queryById(id).then((r) => {
    //   this.doc = r;
    //   this.doc.type = r.type.length > 0 ? r.type[0] : 1;
    // });
  }

  delete(id: any) {
    this.commonservice.confirm('确认删除吗？', () => {
      this.winelist.delete(id).then((res) => {
        this.commonservice.alert('删除成功');
        this.searchData();
      });
    });
  }
  lock(id: any) {
    this.commonservice.confirm('下架后所有餐厅将不再供应此酒饮！确认全线下架吗？', () => {
      this.winelist.lock(id).then((res) => {
        this.commonservice.alert('下架成功');
        this.searchData();
      });
    });
  }
  open(id: any) {
    this.commonservice.confirm('上架后所有餐厅将开始供应此酒饮！确认全线上架吗？', () => {
      this.winelist.open(id).then((res) => {
        this.commonservice.alert('上架成功');
        this.searchData();
      });
    });
  }
  beforeUpload = (file: File) => {
    debugger;
    const isJPG = file.type === 'image/jpeg' || 'image/png';
    if (!isJPG) {
      this.msg.error('您只能上传图片文件!');
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
      this.msg.error('图片大小不能超过 2MB!');
    }
    return isJPG && isLt2M;
  }

  pichandleChange(info: { file: UploadFile }, url: string): void {
    debugger;
    if (info.file.status === 'uploading') {
      this.loading = true;
      return;
    }
    if (info.file.status === 'done') {
      // Get this url from response in real world.
      this.getBase64(info.file.originFileObj, (img: string) => {
        this.loading = false;
        if (info.file && info.file.response && info.file.response.data) {
          this.doc.img = info.file.response.data;
        }
      });
    }
  }
}
